package cn.iocoder.yudao.module.written.exam.controller.admin.userwrittenexamrecords;

import cn.iocoder.yudao.module.written.exam.controller.admin.userwrittenexamrecords.vo.UserWrittenExamRecordsPageReqVO;
import cn.iocoder.yudao.module.written.exam.controller.admin.userwrittenexamrecords.vo.UserWrittenExamRecordsRespVO;
import cn.iocoder.yudao.module.written.exam.controller.admin.userwrittenexamrecords.vo.UserWrittenExamRecordsSaveReqVO;
import cn.iocoder.yudao.module.written.exam.controller.admin.userwrittenexamrecords.vo.UserWrittenExamRecordsUpdateReqVO;
import cn.iocoder.yudao.module.written.exam.dal.dataobject.userwrittenexamrecords.UserWrittenExamRecordsDO;
import cn.iocoder.yudao.module.written.exam.service.userwrittenexamrecords.UserWrittenExamRecordsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 笔试记录")
@RestController
@RequestMapping("/written-exam/user-written-exam-records")
@Validated
public class UserWrittenExamRecordsController {

    @Resource
    private UserWrittenExamRecordsService userWrittenExamRecordsService;

    @PostMapping("/create")
    @Operation(summary = "创建笔试记录")
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records:create')")
    public CommonResult<Long> createUserWrittenExamRecords(@Valid @RequestBody UserWrittenExamRecordsSaveReqVO createReqVO) {
        return success(userWrittenExamRecordsService.createUserWrittenExamRecords(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新笔试记录")
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records:update')")
    public CommonResult<Boolean> updateUserWrittenExamRecords(@Valid @RequestBody UserWrittenExamRecordsUpdateReqVO updateReqVO) {
        userWrittenExamRecordsService.updateUserWrittenExamRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除笔试记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records:delete')")
    public CommonResult<Boolean> deleteUserWrittenExamRecords(@RequestParam("id") Long id) {
        userWrittenExamRecordsService.deleteUserWrittenExamRecords(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得笔试记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records:query')")
    public CommonResult<UserWrittenExamRecordsRespVO> getUserWrittenExamRecords(@RequestParam("id") Long id) {
        UserWrittenExamRecordsDO userWrittenExamRecords = userWrittenExamRecordsService.getUserWrittenExamRecords(id);
        return success(BeanUtils.toBean(userWrittenExamRecords, UserWrittenExamRecordsRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得笔试记录分页")
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records:query')")
    public CommonResult<PageResult<UserWrittenExamRecordsRespVO>> getUserWrittenExamRecordsPage(@Valid @RequestBody UserWrittenExamRecordsPageReqVO pageReqVO) {
        return success(userWrittenExamRecordsService.getUserWrittenExamRecordsPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出笔试记录 Excel")
    @PreAuthorize("@ss.hasPermission('written-exam:user-written-exam-records:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserWrittenExamRecordsExcel(@Valid UserWrittenExamRecordsPageReqVO pageReqVO,
                                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserWrittenExamRecordsRespVO> list = userWrittenExamRecordsService.getUserWrittenExamRecordsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "笔试记录.xls", "数据", UserWrittenExamRecordsRespVO.class, list);
    }

}